package com.example.epic.controller;

import com.example.epic.bean.InAndOutRecord;
import com.example.epic.bean.mq.MqSender;
import com.example.epic.common.ResponseData;
import com.example.epic.service.ControlLevelService;
import com.example.epic.service.InAndOutRecordService;
import com.example.epic.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/wx")
public class InAndOutRecordController {

    @Autowired
    InAndOutRecordService inAndOutRecordService;

    @Autowired
    MqSender mqSender;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    ControlLevelService controlLevelService;


    @PostMapping(value = "/inOut")
    @ResponseBody
    public ResponseData saveInOutRecord(InAndOutRecord inAndOutRecord){
        // 查询防控等级
        Integer levelStatus = (Integer) redisUtils.get("controlLevel");
        if(levelStatus == null){
            levelStatus = controlLevelService.getControlLevel();
            redisUtils.set("controlLevel", levelStatus);
        }

        // 严控状态，进去后七天后才能出
        if(levelStatus == 3){
            ResponseData.fail("目前校内处于隔离状态，不能外出");
        }
        // 不能直接扫码，需要出示申请
        else if(levelStatus == 2){
            return ResponseData.fail("需要申请才能外出");
        }

        mqSender.sendInOutRecord(inAndOutRecord);
        return ResponseData.success("提交出入记录成功");
    }



}
